package day36.edu;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

// Created: 06/10/2025, ChangJiang Ru
public class TcpTest1 {
    @Test
    // Client
    public void test1(){
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            // Create socket
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 9090;
            socket = new Socket(inetAddress, port);
            // create instance of File, instance of FileInputStream
            File file = new File("Hacker.jpg");
            fis = new FileInputStream(file);

            // through socket, receive output stream
            os = socket.getOutputStream();

            // read and write data
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1)
            {
                os.write(buffer, 0, len);
            }
            System.out.println("Data put successfully");
            // client
            socket.shutdownOutput();

            System.out.println("Data have receiving");

            InputStream is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[5];
            int len1;
            while((len1 = is.read(buffer1)) != -1)
            {
                baos.write(buffer1, 0, len1);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close socket and stream
            try {
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    // Server
    public void test2() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            // Create ServerSocket
            int port = 9090;
            serverSocket = new ServerSocket(port);

            // Receiving from the data of client, create socket accept
            socket = serverSocket.accept();
            System.out.println(socket.getInetAddress().getHostAddress());

            // through socket receiving the input stream
            is = socket.getInputStream();

            // create instance of file, instance of FileOutputStream
            File file = new File("Hacker_copy1.jpg");
            fos = new FileOutputStream(file);

            // read and write data
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1)
            {
                fos.write(buffer, 0, len);
            }

            // server receive the data to client
            OutputStream os = socket.getOutputStream();
            os.write("Do you receiving the picture".getBytes());

            System.out.println("\nData receiving successfully");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close socket and stream
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (serverSocket != null)
                    serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
